<!DOCTYPE html>
<html class="x-admin-sm">

<head>
  <meta charset="UTF-8">
  <title></title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <link rel="stylesheet" href="/static/css/font.css">
  <link rel="stylesheet" href="/static/css/xadmin.css">
  <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
  <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
  <script type="text/javascript" src="/static/js/xadmin.js"></script>
  <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
  <!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>

<body>

<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body ">
          <form class="layui-form layui-col-space5" id="myform" onsubmit="return false">
            <div class="layui-input-inline layui-show-xs-block">
              <input class="layui-input" placeholder="分类名" name="title"></div>
            <input type="hidden" name="pid" value="0">
            <div class="layui-input-inline layui-show-xs-block">
              <button class="layui-btn"  onclick="addCate()"><i class="layui-icon"></i>增加</button>
            </div>
          </form>
          <hr>
          <a class="layui-btn layui-btn-sm"  onclick="F_Open_dialog()">导入分组</a>
          <a class="layui-btn layui-btn-sm"  href="/uploads/模板.xlsx">下载分组模板</a>
        </div>

        <div class="layui-card-body ">
          <table class="layui-table layui-form">
            <thead>
            <tr>

              <th width="70">ID</th>
              <th>分类名称</th>
              <th width="80">状态</th>
              <th width="250">操作</th>
            </thead>
            <tbody class="x-cate">
            {volist name="cate" id="v"}
            <tr cate-id="{$v.id}" fid={$v.pid} >

              <td>{$v.id}</td>
              <td>
                <i class="layui-icon x-show" status='true'>&#xe623;</i>
                <?php echo $v['title']?>
              </td>
              <td>
                <button class="layui-btn layui-btn layui-btn-xs"  n>{if $v.status == 1}正常{else/}已禁用{/if}</button>
              </td>
              <td class="td-manage">
                <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑','editCate/?id={$v.id}',600,400)" >
                  <i class="layui-icon">&#xe642;</i>编辑
                </button>
                <button class="layui-btn layui-btn-warm layui-btn-xs"  onclick="xadmin.open('编辑','addCate/?pid={$v.id}',600,400)" >
                  <i class="layui-icon">&#xe642;</i>添加子分类
                </button>
                <button class="layui-btn layui-btn-danger layui-btn-xs"  onclick="disCate({$v.id})" >
                  删除
                </button>
              </td>
            </tr>
            {/volist}
            </tbody>
          </table>
        </div>
        <div class="layui-card-body ">
          <div class="page">
            <div>

            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<input type="file" id="btn_file" onchange="upexcel()" style="display:none">
<script>
  function F_Open_dialog(){
    document.getElementById("btn_file").click();
  }
  function upexcel(){
    layer.confirm('确认提交?', function(index){
      var formData = new FormData();

      formData.append("file",$('#btn_file')[0].files[0]);
      $.ajax({
        url:'/index/group/excelrun',
        dataType:'json',
        type:'POST',
        async: false,
        data: formData,
        processData : false, // 使数据不做处理
        contentType : false, // 不要设置Content-Type请求头
        success: function(data){
          console.log(data);
          if (data.status == '1') {
            alert('上传成功！');
          }else{
            alert(data.msg);
          }

        },
        error:function(response){
          console.log(response);
        }
      });

      layer.close(index);
    });


  }
  layui.use(['form', 'layer'], function(){
    form = layui.form;


  });

  function addCate() {
    $.ajax({
      url:"/index/group/addCate",
      type:"post",
      data:$("#myform").serialize(),
      dataType:"json",
      success:function (res) {
        if (res.code){
          layer.alert(res.msg, {
            icon: 6
          }, function(){
            location.href=""
          })
        }else{
          layer.alert(res.msg, {
            icon: 5
          })
        }
      }
    })

  }

  function disCate(id) {
    $.ajax({
      url:"/index/Group/disCate",
      type:"post",
      data:{id:id},
      dataType:"json",
      success:function (res) {
        if (res.code){
          layer.alert('操作成功', {
                    icon: 6
                  },
                  function () {
                    window.location.href="";
                  })
        }else{
          layer.alert('网络开小差了', {
            icon: 5
          })
        }
      }
    })

  }



  // 分类展开收起的分类的逻辑
  //
  $(function(){
    $("tbody.x-cate tr[fid!='0']").hide();
    // 栏目多级显示效果
    $('.x-show').click(function () {
      if($(this).attr('status')=='true'){
        $(this).html('&#xe625;');
        $(this).attr('status','false');
        cateId = $(this).parents('tr').attr('cate-id');
        $("tbody tr[fid="+cateId+"]").show();
      }else{
        cateIds = [];
        $(this).html('&#xe623;');
        $(this).attr('status','true');
        cateId = $(this).parents('tr').attr('cate-id');
        getCateId(cateId);
        for (var i in cateIds) {
          $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
        }
      }
    })
  })

  var cateIds = [];
  function getCateId(cateId) {
    $("tbody tr[fid="+cateId+"]").each(function(index, el) {
      id = $(el).attr('cate-id');
      cateIds.push(id);
      getCateId(id);
    });
  }

</script>
</body>
</html>
